package com.xjoyt.pay.master.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import com.xjoyt.pay.master.dao.DistributorProfitRepositoryCustom;
import com.xjoyt.pay.master.po.DistributorProfit;
import com.xjoyt.pay.repository.Repository;

public interface DistributorProfitRepository extends Repository<DistributorProfit>, DistributorProfitRepositoryCustom {

	Page<DistributorProfit> findAllByOrderIdAndDistributorId(String orderId, String distributorId, Pageable pageable);

	// @Query(" select a.distributor.id, sum(a.money), sum( (a.rate -
	// a.costRate) * a.money / 1000 ) from DistributorProfit a where
	// a.distributor.id in ?1 and a.addedDate >= ?2 and a.addedDate <= ?3 group
	// by a.distributor.id")
	@Query(" select a.distributor.id, sum(a.money), sum( (a.rate - a.costRate) * a.money / 100 ) from DistributorProfit a where a.distributor.id in ?1 and a.addedDate >= ?2 and a.addedDate <= ?3 group by a.distributor.id")
	List<Object[]> sumByDistributorIdInGroupByDistributorIdIn(List<String> distributorIds, Date startDate,
			Date endDate);

	// @Query(" select a.distributor.id, sum(a.money), sum( (a.rate -
	// a.costRate) * a.money / 1000 ) from DistributorProfit a where
	// a.distributor.id in ?1 group by a.distributor.id")
	@Query(" select a.distributor.id, sum(a.money), sum( (a.rate - a.costRate) * a.money / 100 ) from DistributorProfit a where a.distributor.id in ?1 group by a.distributor.id")
	List<Object[]> sumByDistributorIdInGroupByDistributorIdIn(List<String> distributorIds);
}
